<?php
/**
 *	Autocomplete Facebook Style Plugin
 *	@package autocomplete facebook style
 *	@author Liran Tal <liran.tal@gmail.com>
 *	@license GNU General Public License (GPL) version 2
 *	@copyright (c) Liran Tal of Enginx 2009
 *	@link http://www.enginx.com
 **/
?>
<!-- autocomplete facebook style plugin by Liran Tal of Enginx http://www.enginx.com -->


<?php

// create a unique token to add to the input element's id
// in case there are a couple of instances of this input type
// so that the javascript code doesn't get all drunk (confused)
// while using uniqid() is probably better it is also slower
$token = rand(0,10000);

?>


<?php
	$xray_baseurl = elgg_get_site_url()."xray";
	$readonly = $vars['readonly'];
	$css = $vars['css'];
	$width = $vars['width'];
	$height = $vars['height'];
	$class = $vars['class'];
	if (!$class)
		$class = "input-text";
	
	//force the autocomplete class
	$class = "autocomplete";
	
	$value = $vars['value'];
	
	$name = $vars['name'];
	if (!$name)
		$name = "autocomplete_facebook_style".$token;

?>


<script language="javascript" type="text/javascript" src="<?php echo $vars['url']; ?>mod/autocomplete_facebook_style/vendors/autocomplete_fcbkcomplete/jquery.fcbkcomplete.js"></script>
<script language="JavaScript">
<?php 
if ($name=='my_focused') {
?>	  
function add_link() {
}
function adjust_postion() {
	var obj = $("#<?=$name?>+ul+div.facebook-auto");
	var input = $("#<?=$name?>+ul li#my_focused_annoninput");
	var top = input.position().top+20;
	obj.css({'top':top});
	console.log('adjust_postion');
}
function <?=$name?>_onselect(item) {
	//console.log('<?=$name?>_onselect');
	//console.dir(item);
	//console.log(item._value);
	adjust_postion();
	$("select#<?=$name?> option").each(function(){
		//console.log($(this).val());
		//console.log($(this).text());
		if ($(this).val()==item._value) {
			var item_name = $(this).text();
			item_name = item_name.replace(item._value+"|", "");
			//console.log(item_name);
			$.post("<?php echo elgg_get_site_url();?>focus/add",{sec:item._value,sec_name:item_name},function(result){
				//var result_str = item._value+" aded to foucsed list";
				//console.log(result);
				//$("div#<?=$name?>_text").html(result_str).show().fadeTo(3000,0.1,function(){$(this).hide()});
			});			
		}
	});

}
function <?=$name?>_onremove(item) {
	console.log('<?=$name?>_onselect');
	console.dir(item);
	console.log(item._value);
	adjust_postion();
	$.post("<?php echo elgg_get_site_url();?>/focus/del",{sec:item._value},function(result){
		console.log(result);
	});
}

<?php 
}
?>

	$(document).ready(function() 
	{        
	 
	  $("#<?=$name?>").fcbkcomplete({
		json_url: "<?php echo $vars['url']; ?>mod/autocomplete_facebook_style/autocomplete_securities.php",
		cache: false,
		filter_case: true,
		filter_hide: true,
		firstselected: true,
		filter_selected: false,
		newel: false,        
        onselect: "<?=$name?>_onselect",
        onremove: "<?=$name?>_onremove",
	  });		 
		$(".facebook-auto").css('z-index','9999');
  <?php 
  if ($css) {
  ?>	  
	  $("#<?=$name?>+ul.holder").css(<?php echo $css; ?>);
  <?php 
  }
  if ($width) {
  ?>	  
	  $("#<?=$name?>+ul.holder").width('<?php echo $width; ?>');
  <?php 
  }
  if ($height) {
  ?>	  
	  $("#<?=$name?>+ul.holder").css('min-height','<?php echo $height; ?>');
  <?php 
  }
  if ($readonly) {
  ?>	  
		$("#<?=$name?>+ul li.bit-input").hide();
		$("#<?=$name?>+ul li a.closebutton").hide();
		$("#<?=$name?>+ul.holder").unbind();
  <?php 
  }
  if ($name=='my_focused') {
  ?>	  
	$("#<?=$name?>+ul.holder").click(function() {
		adjust_postion();
	});
<?php 
  }
  ?>
	$("#<?=$name?>+ul li.bit-box").live("hover", function(event) {
		var code = $(this).attr('rel');
		var str = $(this).text();

		var url = "<?php echo $xray_baseurl; ?>/"+code;
		var a_str = "<a href='<?php echo $xray_baseurl; ?>/"+code+"' target='_blank'>"+str+"</a>";
		var close_obg = $(this).children("a.closebutton");
	
		var span = $(this).children("span");
		if (span.length==0) {
			$(this).wrapInner("<span style='display:none'></span>");
			$(this).prepend(a_str);
			$(this).append(close_obg);
		} 
		var span = $(this).children("span");
		var a_obj = $(this).children("a[href$='"+code+"']");

		if (event.type=="mouseenter" || event.type=="mouseover") {
			a_obj.show();
			span.hide();
		} else {
			a_obj.hide();
			span.show();
		}
		
 <?php 
  if ($readonly) {
  ?>	  
		$("#<?=$name?>+ul li a.closebutton").hide();
  <?php 
  }
  ?>	  
	});
	
	});
</script>

<div id="<?=$name?>_text" style="display:none;"></div>
<div style="color:#4690D6;<?php if($name=='my_focused'||$name=='all_focused') echo "overflow-y:scroll;height:250px;";?>">
<input type="hidden" name="<?=$name?>" value="" />
      <select id="<?=$name?>" name="<?=$name?>">
<?php
if (is_array($value)) {
	foreach ($value as $code) {
		$code_name = get_securities_name_by_code($code);
		$options_values[$code] = "$code $code_name";
	}
} else if($value) {
	$code = $value;
		$code_name = get_securities_name_by_code($code);
		$options_values[$code] = "$code $code_name";
} else {}

if ($options_values) {
	foreach ($options_values as $opt_value => $option) {

		$option_attrs = elgg_format_attributes(array(
			'value' => $opt_value,
			'selected' => (string)$opt_value == (string)$value,
		));

		echo "<option selected=\"selected\" class=\"selected\" $option_attrs>$option</option>";
	}
}
?>
      </select>
</div>
